package com.zmn.plat.admin.controller.difficult.ratio;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.base.difficult.ratio.DifficultRatio;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2021/1/8 18:27
 **/
@Controller
@RequestMapping("/difficult/ratio/")
@Slf4j
public class DifficultRatioModifyController extends DifficultRatioBaseController {

    /**
     * region 新增/修改
     *
     * @param difficultRatio
     * @return
     */
    @SuppressWarnings("Duplicates")
    @RequiresPermissions("/difficult/ratio/detail.action")
    @RequestMapping(value = "addEdit")
    @ResponseBody
    public ResponseDTO addEdit(@RequestBody DifficultRatio difficultRatio) {
        Date now = DateUtil.getNow();

        Integer difficultId = difficultRatio.getDifficultId();

        try {
            if (NumberUtil.isNullOrZero(difficultId)) {
                difficultRatio.setCreater(getMcStaffRealName());
                difficultRatio.setCreateTime(now);
                difficultRatioService.insert(difficultRatio);
            } else {
                difficultRatio.setUpdater(getMcStaffRealName());
                difficultRatio.setUpdateTime(now);
                difficultRatioService.updateByKey(difficultRatio);
            }

            return ResponseDTO.success(Integer.toString(difficultRatio.getDifficultId()), "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            log.error("难度系数【{}】已经存在，不能重复添加！", difficultRatio.getDifficultName());
            return ResponseDTO.fail("名称或者系数已经存在，不能重复添加！");
        } catch (Exception ex) {
            log.error(String.format("[zmn]新增/修改难度系数失败,area:[%s],ex:[%s]", JSONObject.toJSONString(difficultRatio), ex.getMessage()));
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequiresPermissions("/difficult/ratio/delete.action")
    @RequestMapping(value = "delete")
    @ResponseBody
    public ResponseDTO updateStatus(@RequestParam(value = "ids", defaultValue = "") String ids) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        difficultRatioService.deleteByKeys(arrayIds);
        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, GlobalConsts.SUCCESS_MSG);
    }
}
